home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / symlink.c < prev    next >
C/C++ Source or Header  |  1993-10-20  |  1KB  |  80 lines

  1. /* soft link routines */
  2.  
  3. #include <mintbind.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <limits.h>
  7. #include <unistd.h>
  8. #include <stat.h>
  9. #include "lib.h"
  10.  
  11. extern int __mint;
  12.  
  13. /*
  14.  * If MiNT 0.9 or later is active, use the kernel routines for these;
  15.  * otherwise, try to choose failure modes that applications will best be
  16.  * able to handle
  17.  */
  18.  
  19. int
  20. symlink(old, new)
  21.     const char *old, *new;
  22. {
  23.     char linkname[PATH_MAX];
  24.     char path[PATH_MAX];
  25.     long r;
  26.  
  27.     if (__mint >= 9) {
  28.         _unx2dos(old, path);
  29.         _unx2dos(new, linkname);
  30.         r = Fsymlink(path, linkname);
  31.         if (r) {
  32.             errno = (int) -r;
  33.             return -1;
  34.         }
  35.         return (int) r;
  36.     }
  37.     errno = EINVAL;
  38.     return -1;
  39. }
  40.  
  41. int
  42. readlink(unxname, buf, siz)
  43.     char *unxname, *buf;
  44.     int siz;
  45. {
  46.     long r;
  47.     size_t l;
  48.     char filename[PATH_MAX];
  49.     char linkto[PATH_MAX+1];
  50.  
  51.     if (__mint < 9) {
  52.         errno = EINVAL;
  53.         return -1;
  54.     }
  55.     _unx2dos(unxname, filename);
  56.     r = Freadlink(PATH_MAX, linkto, filename);
  57.     if (r < 0) {
  58.         if (r == -EACCES) {
  59.             struct stat sb;
  60.  
  61.             /* UNIX gives EINVAL, not EACCES, on non-links */
  62.             if ((Fxattr(1, filename, &sb) == 0)
  63.                 && ((sb.st_mode & S_IFMT) != S_IFLNK)) {
  64.                 r = -EINVAL;
  65.             }
  66.         }
  67.         errno = (int) -r;
  68.         return -1;
  69.     }
  70.     linkto[PATH_MAX] = 0;
  71.     _dos2unx(linkto, filename);
  72.     l = strlen(filename);
  73.     if (l > siz) {
  74.         errno = ERANGE;
  75.         return -1;
  76.     }
  77.     strncpy(buf, filename, siz);
  78.     return (int) l;
  79. }
  80.